package com.sgb.kjwl.viewmodel.transport;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.model.entity.transport.MapLongAndLatEntity;
import com.sgb.kjwl.model.entity.transport.MapSourcesEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.repository.TransportRepertory;
import com.sgb.kjwl.view.ui.activity.transport.MapGoodsActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MapGoodsModel extends BaseViewModel {
    private MapGoodsActivity activity;
    private TransportRepertory repertory;
    private int page = 1;
    private int size = 10;
    public int total;

    public MapGoodsModel(MapGoodsActivity activity, TransportRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void getAddressModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                activity.initAddressData(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getLL(String provinceNo,String cityNo,String districtNo){
        APIManager.getInstance().getDriverAPI().getLocationLL(repertory.getAreaCode(),provinceNo,cityNo,districtNo).enqueue(new Callback<BaseEntity<MapLongAndLatEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<MapLongAndLatEntity>> call, Response<BaseEntity<MapLongAndLatEntity>> response) {
                if (response.body() == null) return;

                if (response.body().getState().equals("ok")) {
                    activity.getLL(response.body().getData());
                } else {
//                    MToast.showToast(activity, response.body().getMsg());
                }
            }
            @Override
            public void onFailure(Call<BaseEntity<MapLongAndLatEntity>> call, Throwable t) {
            }
        });
    }

    public void getRadiusList(){
        APIManager.getInstance().getDriverAPI().getRadiusList().enqueue(new Callback<BaseEntity<List<GoodsInfoTypeEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Response<BaseEntity<List<GoodsInfoTypeEntity3>>> response) {
                if (response.body() == null) return;

                if (response.body().getState().equals("ok")) {
                    activity.getRadiusList(response.body().getData());
                } else {
//                    MToast.showToast(activity, response.body().getMsg());
                }
            }
            @Override
            public void onFailure(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Throwable t) {
            }
        });
    }

    public void getMapList(String l,String t,String d){
        APIManager.getInstance().getDriverAPI().getMapSourcesList(repertory.getAreaCode(), l, t, d).enqueue(new Callback<BaseEntity<List<MapSourcesEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<MapSourcesEntity>>> call, Response<BaseEntity<List<MapSourcesEntity>>> response) {
                if (response.body() == null) return;

                if (response.body().getState().equals("ok")) {
//                    Log.v("responsBody===",new Gson().toJson(response.body().getData()));
                    activity.showMapList(response.body().getData());
                } else {
//                    MToast.showToast(activity, response.body().getMsg());
                }
            }
            @Override
            public void onFailure(Call<BaseEntity<List<MapSourcesEntity>>> call, Throwable t) {
            }
        });
    }

}
